package filter;

import Utils.BaseMessage;
import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 禁止登陆后查看
 */
@WebFilter(filterName = "isNoLoginFilter", urlPatterns = {
        "/api/User/isNoLogin/*",
        "/api/CreditCard/isNoLogin/*",
        "/api/Bank/isNoLogin/*"
})
public class isNoLoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession();
        if (session.getAttribute("userMsg") != null) {
            response.getWriter().print(JSON.toJSONString(new BaseMessage(1, "您已登录,禁止操作", null)));
            return;
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
